﻿@using App.Web.Utility;
@using App.Core.Extensions;
@using App.Core.DB;
@{
    bool _ShowEditLink = ViewBag.ShowEditLink == null ? false : NullHandler.GetBoolean(ViewBag.ShowEditLink);
    ///By default Add to Wishlist will be shown in the UI
    bool _ShowAddToWishlist = ViewBag.ShowAddToWishlist == null  ? true : NullHandler.GetBoolean(ViewBag.ShowAddToWishlist);
    String _LinkTarget = String.Empty;
    if(_ShowEditLink)
    {        
        _LinkTarget = " target=\"_blank\"";
    }
    System.Data.DataTable dt = ViewBag.DataSource as System.Data.DataTable;
    int totalRecord = dt != null ? dt.Rows.Count : 0;//NullHandler.GetInt(ViewBag.TotalRecord);
    int[] ProductCompareList = (from P in SessionCache.ProductCompareList select P.ID).ToArray();
    String _CompanyKey = ViewData[AppConstants.ViewDataKeys.COMPANY_KEY] as String;
    bool _ShowMessage = ViewBag.ShowMessage != null ? false : true ;
    Dictionary<int, int> _Occurance = new Dictionary<int, int>();
    bool _CalculateOccurance = ViewData[AppConstants.ViewDataKeys.CALCULATE_OCCURANCE] == null ? false : true; 
}

@if (totalRecord > 0)
{
    int loopCounter = 0;
    <div class="product_description">
    @foreach (System.Data.DataRow product in dt.Rows)
    {
        int productID = NullHandler.GetInt(product["ID"]);
        String compareClass = ProductCompareList.Contains(productID) ? "product-compared-link-button" : "product-compare-link-button";
        String compareText = compareClass == "product-compared-link-button" ? "Compare (-)" : "Compare (+)";
        String thumbnail = NullHandler.GetString(product["Thumbnail"]);
        loopCounter++;
        if (_CalculateOccurance)
        {
            int categoryID = NullHandler.GetInt(product["CategoryID"]);
            if (_Occurance.Keys.Contains(categoryID))
            {
                _Occurance[categoryID]++;
            }
            else
            {
                _Occurance.Add(categoryID, 1);
            }
        }
        <div class="features">
            <div class="image-checkbox">
                <div class="image">
                    @Html.ProductThumb(thumbnail, productID, NullHandler.GetString(product["Name"]))                    
                </div>
                <div class="checkbox">
                    @if(compareClass == "product-compared-link-button"){                        
                        <a class="@compareClass" href="javascript:void(0);" onclick="RemoveFromProductCompare(@productID, this);">@compareText</a>
                    }
                    else{                        
                        <a class="@compareClass" href="javascript:void(0);" onclick="AddToProductCompare(@productID, this);">@compareText</a> 
                    }
                </div>
                                     
            </div>
            <div class="details-block">
                <div class="details_top">
                    @if(_CompanyKey.IsNullOrEmpty()){
                    <a href="/product/details/@(productID)/@(product["ProductUrl"])" @_LinkTarget >@product["Name"]</a>
                    }
                    else{
                        @Html.CompanyProductDetailsLink(_CompanyKey, productID, NullHandler.GetString(product["ProductUrl"]), NullHandler.GetString(product["Name"]))
                    }
                    <div>@product["Company"]</div>
                </div>
                <div class="details">@product["Description"].ToString().GetWords(40)</div>
               
            </div>
            <div class="price-block">
                <div>
                    <div class="price-caption">Price</div>
                    <div class="price-amount">@Html.FormatData(product["Price"]) <span>@ConfigReader.CurrencyCode</span></div>
                </div>
                <div class="clear"></div>
                <div>
                    
                </div>
                <div class="offer-block">
                    <img src="/Content/Images/available_tag.png" alt="In Stock" title="In Stock" />
                    
                    @if (_ShowAddToWishlist)
                    {
                        if(NullHandler.GetBoolean(product["WishList"]) == false)
                        {
                            <img id="img@(loopCounter)"
                            src="/Content/Images/AddToCart2.png" 
                            height="20px" width="20px" 
                            title="Add To Favorite" 
                            onclick="AddRemoveWishList('@product["ID"]','img@(loopCounter)', this.title);"
                            onmouseover="this.style.cursor='pointer'" 
                            alt="Add To Favorite" />                            
                        }
                        else
                        {   
                            <img id="img@(loopCounter)" 
                            src="/Content/Images/RemoveFromCart2.png" 
                            height="20px" width="20px" 
                            title="Remove From Favorite" 
                            onclick="AddRemoveWishList('@product["ID"]','img@(loopCounter)', this.title);"
                            onmouseover="this.style.cursor='pointer'" 
                            alt="Remove From Favorite" />                           
                        }
                    }
                
                </div>
                @if (_ShowEditLink)
                {
                <div>                    
                    @Html.PrivateLink("Edit", "Edit/" + productID, "Product")                    
                </div>
                }
            </div>
            <div class="clear"></div>
            <div style="margin-bottom:5px;"></div>
        </div>
    } 
    </div>
    if (_CalculateOccurance){
        int mostCommonValue = 0;
        int highestCount = 0;
        foreach(KeyValuePair<int, int> pair in _Occurance){
            if (pair.Value > highestCount)
            {
                mostCommonValue = pair.Key;
                highestCount = pair.Value;
            }
        }
        <input type="hidden" id="hdnCategoryID" value="@mostCommonValue" />
    }
 
    
<div class="bm-dialog"  id="divMessageBox" style="display:none">
    <div>            
        <div class="bm-dialog-title">Add/Remove From Wish List</div>
    </div>
    <div class="bm-dialog-body" style="min-height:60px;" id="dvMsg">
                             
    </div>
    <div class="bm-dialog-button" style="height:15px;"></div>
</div>    
       
<script language="Javascript" type="text/javascript">
    
    function ShowDialogMessage(Message) {
        var $msgDiv = $('#dvMsg');
        $msgDiv.show().html(Message);

        var $messageDiv = $('#divMessageBox');
        $messageDiv.show();
        setTimeout(function () { $messageDiv.hide(); }, 3000);
    }

    function AddRemoveWishList(productId, btnId, btnText) {
            var url = '@Url.Action("AddRemoveWishList", "Product", new { area = AppConstants.Area.PUBLIC })';
            Ajax(url, 'ID=' + productId, function (isAdded) {
                if (isAdded) {
                    if (btnText == 'Add To Favorite') {
                        $('#' + btnId).attr('title', 'Remove From Favorite');
                        $('#' + btnId).attr('src', '../../Content/Images/RemoveFromCart2.png');
                        $('#' + btnId).attr('alt', 'Remove From Favorite');
                        ShowDialogMessage('Product add to your favorite list.');
                    }
                    else {
                        $('#' + btnId).attr('title', 'Add To Favorite');
                        $('#' + btnId).attr('src', '../../Content/Images/AddToCart2.png');
                        $('#' + btnId).attr('alt', 'Add To Favorite');
                        ShowDialogMessage('Product removed from your favorite list.');
                    }
                }
                else {
                    window.location = "/Account/LogOn?ProductID=" + productId + "&ReqFrom=AddFavorite";
                }
            });
    }

</script>       
}
else if (_ShowMessage)
{
    @Html.ErrorMessageBox("Sorry! No Product Found.")
}

